<?php
class Search_model extends CI_Model {
	
	function search($query_array, $limit, $offset, $sort_by, $sort_order,$num_rows = 0) {
		
     
	    $sort_order = ($sort_order == 'desc') ? 'desc' : 'asc';
        $sort_columns = array('product_id', 'name', 'model', 'standart_price', 'stock', 'brand_name','dateadd');
		$sort_by = (in_array($sort_by, $sort_columns)) ? $sort_by : 'model';
        $include = "";
       // $mod = " seller.mod <> 2 AND ";
		if ($query_array->category_id) {
		    $category_id = $query_array->category_id;
			$include .= " products.category_id = $category_id AND "; 
		}
        if($query_array->subcategory_id)
        {
            $subcategory_id = $query_array->subcategory_id;
            $include .= " products.subcategory_id = $subcategory_id AND ";
        }
        if($query_array->brand_id)
        {
            $brand_id = $query_array->brand_id;
            $include .= " products.brand_id = $brand_id AND ";
        }
        if($query_array->hizlial)
        {
        //    $mod .= " seller.mod = 1 AND ";
        }
        if($query_array->seller_user_id )
        {
        //    $seller_user_id = $query_array->seller_user_id;
         //   $mod .= " seller.user_id = $seller_user_id AND ";
        }
      /*  if($query_array->username != "" )
        {
            $username = $query_array->username;
            if($this->db->where('username',$username)->get('users')->num_rows() > 0)
            $uid = $this->db->where('username',$username)->get('users')->row(0)->user_id;
            $mod .= " seller.user_id = $uid AND ";
        } */       
      /*  if($query_array->fiyat1  OR $query_array->fiyat2  )
        {
            $fiyat1 = $query_array->fiyat1;
            $fiyat2 = $query_array->fiyat2;
            $mod .= " (seller.user_price >= $fiyat1 AND seller.user_price <= $fiyat2) AND ";
        } */ 
       /* if($query_array->kargo  )
        {
            // Databasede kargo 0 demek kargo ücreti satıcıya ait demek
            // Databasede kargo 1 demek kargo ücreti alıcıya ait demek
           if($query_array->kargo == 2)
           {
             $kargo = 0;
           }
           else
           {
             $kargo = 1;
           }
            $mod .= " seller.kargo = $kargo AND ";
        }   */            
        if($num_rows == 0)
        {
             $sql = "SELECT products.*, brands.brand_id FROM products
               JOIN brands ON brands.brand_id = products.brand_id 
               
               WHERE
               $include
               
                
             
               (products.name LIKE ? OR products.model LIKE ? OR brands.name LIKE ? ) 
               ORDER BY $sort_by $sort_order               
               LIMIT $offset, $limit ";
                  
           return $this->db->query($sql,array('%'.$query_array->value.'%','%'.$query_array->value.'%','%'.$query_array->value.'%'));
        }
        else
        {
             $sql = "SELECT products.* , brands.brand_id FROM products
               JOIN brands ON brands.brand_id = products.brand_id
                
               WHERE
               $include
                
              
               (products.name LIKE ? OR products.model LIKE ? OR brands.name LIKE ? ) 
               ";
                  
           return $this->db->query($sql,array('%'.$query_array->value.'%','%'.$query_array->value.'%','%'.$query_array->value.'%')); 
        }
        
         
		// results query

	}
	function kategori_hizli_search($query_array,$limit, $offset, $sort_by, $sort_order,$num_rows = 0)
    { 
        $sort_order = ($sort_order == 'desc') ? 'desc' : 'asc';
        $sort_columns = array('product_id', 'name', 'model', 'user_price', 'stock', 'brand_name','dateadd');
		$sort_by = (in_array($sort_by, $sort_columns)) ? $sort_by : 'model';
                    
        if($num_rows == 0)
        {
             $sql = "SELECT * FROM products
               WHERE
               (products.name LIKE ? OR products.model LIKE ? ) 
               ORDER BY $sort_by $sort_order               
               LIMIT $offset, $limit ";
                  
           return $this->db->query($sql,array('%'.$query_array->value.'%','%'.$query_array->value.'%'));
        }
        else
        {
             $sql = "SELECT * FROM products
                WHERE
                (products.name LIKE ? OR products.model LIKE ? ) 
                ";
                  
           return $this->db->query($sql,array('%'.$query_array->value.'%','%'.$query_array->value.'%')); 
        }
        
    }
	function kategori_normal_search($query_array,$limit, $offset, $sort_by, $sort_order,$num_rows = 0)
    { 
        $sort_order = ($sort_order == 'desc') ? 'desc' : 'asc';
        $sort_columns = array('product_id', 'name', 'model', 'user_price', 'stock', 'brand_name','dateadd');
		$sort_by = (in_array($sort_by, $sort_columns)) ? $sort_by : 'model';
                    
        if($num_rows == 0)
        {
             $sql = "SELECT * FROM products
               WHERE
               (products.name LIKE ? OR products.model LIKE ? ) 
               ORDER BY $sort_by $sort_order               
               LIMIT $offset, $limit ";
                  
           return $this->db->query($sql,array('%'.$query_array->value.'%','%'.$query_array->value.'%'));
        }
        else
        {
             $sql = "SELECT * FROM products
                WHERE
                (products.name LIKE ? OR products.model LIKE ? ) 
                ";
                  
           return $this->db->query($sql,array('%'.$query_array->value.'%','%'.$query_array->value.'%')); 
        }
        
    }  
	function talepler_search($query_array,$limit, $offset, $sort_by, $sort_order,$num_rows = 0)
    { 
        $sort_order = ($sort_order == 'desc') ? 'desc' : 'asc';
        $sort_columns = array('product_id', 'name', 'model', 'user_price', 'stock', 'brand_name','dateadd');
		$sort_by = (in_array($sort_by, $sort_columns)) ? $sort_by : 'model';
                    
        if($num_rows == 0)
        {
             $sql = "SELECT * FROM products
               WHERE
               (products.name LIKE ? OR products.model LIKE ? ) 
               ORDER BY $sort_by $sort_order               
               LIMIT $offset, $limit ";
                  
           return $this->db->query($sql,array('%'.$query_array->value.'%','%'.$query_array->value.'%'));
        }
        else
        {
             $sql = "SELECT * FROM products
                WHERE
                (products.name LIKE ? OR products.model LIKE ? ) 
                ";
                  
           return $this->db->query($sql,array('%'.$query_array->value.'%','%'.$query_array->value.'%')); 
        }
        
    }        

}
